package codebot;

import codebot.processor.IScreen;

import com.google.wave.api.Range;
import com.google.wave.api.TextView;

public final class MyScreen implements IScreen {

  // wave syntax constants
  public final static String ANNOTATION_COLOR = "style/color";
  public final static String ANNOTATION_FONT_STYLE = "style/fontStyle";
  public final static String ANNOTATION_WEIGHT = "style/fontWeight";
  public final static String VALUE_ITALIC = "italic";
  public final static String VALUE_NORMAL = "normal";
  public final static String VALUE_WEIGHT_BOLD = "bold";

  private static TextView sTv;
  private static int sLogCount;
  private static String sLastLogMessage;

  /**
   * Constructs the 'screen' by giving it the connection to Wave (the TextView
   * object)
   * 
   * @param tv
   */
  public MyScreen(TextView tv) {
    sTv = tv;
    sLogCount = 0;
    sLastLogMessage="";
  }

  @Override
  public String getText() {
    return sTv.getText();
  }

  @Override
  public String getText(int start, int end) {
    return sTv.getText(new Range(start, end));
  }
  
  @Override
  public void insert(int where, String what){
    sTv.insert(where, what);
  }

  @Override
  public void log(String message) {
    sLogCount++;
    if (sLastLogMessage!=message){
      sLastLogMessage=message;
      sTv.append("\n|" + sLogCount + '-' + message);
    }
  }

  @Override
  public void replace(int start, int end, String newText) {
    sTv.replace(new Range(start, end+1), newText);
  }

  @Override
  public void setBold(boolean b, int start, int end) {
    sTv.setAnnotation(new Range(start, end+1), ANNOTATION_WEIGHT,
        b ? VALUE_WEIGHT_BOLD : VALUE_NORMAL);
  }

  @Override
  public void setColor(String rgb, int start, int end) {
    sTv.setAnnotation(new Range(start, end+1), ANNOTATION_COLOR, rgb);
  }

  @Override
  public void setFont(String font, int start, int end) {
    sTv.setAnnotation(new Range(start, end+1), ANNOTATION_FONT_STYLE, font);

  }

  @Override
  public void setItalic(boolean b, int start, int end) {
    sTv.setAnnotation(new Range(start, end+1), ANNOTATION_FONT_STYLE,
        b ? VALUE_ITALIC : VALUE_NORMAL);
  }
}
